package com.zjw.zy.base;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.zjw.zy.R;
import com.zjw.zy.base.BaseViewGroup;
import com.zjw.zy.utils.AnimationUtils;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/10/9 13:20
 */
public abstract class BaseExpandView extends BaseViewGroup {

    private FrameLayout left;
    private FrameLayout right;
    private boolean expand = false;

    public BaseExpandView(Context context) {
        super(context);
    }

    public BaseExpandView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BaseExpandView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected int onLayoutId() {
        return R.layout.expand_view;
    }

    @Override
    protected void onBindListener() {
        this.setOnClickListener(v -> {
            if (expand) {
                unExpand();
            }
        });
    }

    @Override
    protected void onBindValue() {
        left.addView(onLeftView(this));
        right.addView(onRightView(this));
    }

    @Override
    protected void onFindView(View rootView) {
        left = findViewById(R.id.expand_left);
        right = findViewById(R.id.expand_right);
    }


    public void expand() {
        AnimationUtils.objectAnimationOfX(left, -150f)
                .addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation, boolean isReverse) {
                        right.setVisibility(View.VISIBLE);
                        expand = true;
                    }
                }).autoCancel(false)
                .start();
    }

    public void unExpand() {
        AnimationUtils.objectAnimationOfX(left, 0f)
                .addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation, boolean isReverse) {
                        right.setVisibility(View.GONE);
                        expand = false;
                    }
                }).autoCancel(false)
                .start();
    }


    protected abstract View onRightView(ViewGroup parent);

    protected abstract View onLeftView(ViewGroup parent);

}
